package com.iwdnb.gkgz.controller;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.iwdnb.bmnf.common.model.result.Result;
import com.iwdnb.bmnf.common.service.CacheService;
import com.iwdnb.bmnf.common.utils.BooleanUtils;
import com.iwdnb.gkgz.application.model.vo.StockType;
import com.iwdnb.gkgz.application.service.HistoryDataService;
import com.iwdnb.gkgz.application.service.StockDayDataSerivce;
import com.iwdnb.gkgz.application.service.StockService;
import com.iwdnb.gkgz.common.model.dto.Stock;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StrategyTradeDTO;
import com.iwdnb.gkgz.common.utils.StockRegionUtils;
import com.iwdnb.gkgz.common.utils.StockUtils;
import com.iwdnb.gkgz.infrastructure.dao.client.BiyingClient;
import com.iwdnb.gkgz.infrastructure.dao.client.MairuiClient;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STRATEGY_TRADE;
import static com.iwdnb.gkgz.common.constant.GkgzConstants.ZS_CODE;

/**
 * 股票指数数据处理
 **/
@RestController
@RequestMapping("/api/stock/zs")
@Slf4j
public class StockZsController {

    @Autowired
    private BiyingClient biyingClient;
    @Autowired
    private MairuiClient mairuiClient;
    @Autowired
    private StockDayDataSerivce stockDayDataSerivce;
    @Autowired
    protected MetaDataDAO metaDataDAO;
    @Autowired
    private Executor executor;

    /**
     * 初始化指数列表
     *
     * @param code
     * @return
     */
    @RequestMapping("/initZsList")
    public Result<String> initZsList() {
        biyingClient.initZsList();
        return Result.getSuccessResult(null);
    }

    /**
     * 加载指数日K线数据,中证1000为sh000852
     *
     * @param code
     * @return
     */
    @RequestMapping("/loadZsStockDayData")
    public Result<String> loadZsStockDayData(String code) {
        if (StringUtils.isBlank(code)) {
            //中证1000为sh000852
            code = "sh000852";
        }
        List<String> codes = Lists.newArrayList(code.split(","));
        for (String c : codes) {
            executor.execute(() -> {
                biyingClient.initZsStockDayDataList(c);
            });
        }
        return Result.getSuccessResult(null);
    }

    ///**
    // * 麦瑞加载指数日K线数据
    // *
    // * @param code
    // * @return
    // */
    //@RequestMapping("/loadMairuiZsStockDayData")
    //public Result<String> loadMairuiZsStockDayData(String code) {
    //    List<String> codes = Lists.newArrayList(code.split(","));
    //    for (String c : codes) {
    //        executor.execute(() -> {
    //            mairuiClient.initZsStockDayDataList(c);
    //        });
    //    }
    //    return Result.getSuccessResult(null);
    //}

    /**
     * 计算操作区间
     *
     * @param code
     * @return
     */
    @RequestMapping("/calculateOperateRegions")
    public Result<String> calculateOperateRegions(String code, String loadDataFlag, String beginDate, String endDate) {
        if (BooleanUtils.isTrue(loadDataFlag)) {
            biyingClient.initZsStockDayDataList(code);
        }
        List<StockDayData> stockDayDataList = stockDayDataSerivce.queryStockDayDataList(code);
        stockDayDataList = StockUtils.getStockDayDataListByDateRange(stockDayDataList, beginDate, endDate);
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("code", code);
        metaQuery.addParam("uuid", ZS_CODE);
        List<StrategyTradeDTO> oldList = metaDataDAO.queryListByCondition(OBJECT_CODE_STRATEGY_TRADE, metaQuery,
            StrategyTradeDTO.class);
        if (CollectionUtils.isNotEmpty(oldList)) {
            metaDataDAO.delete(OBJECT_CODE_STRATEGY_TRADE,
                oldList.stream().map(StrategyTradeDTO::getId).collect(Collectors.toList()));
        }
        List<StrategyTradeDTO> strategyTradeDTOList = StockRegionUtils.calculateRegions(code, stockDayDataList);
        metaDataDAO.insert(OBJECT_CODE_STRATEGY_TRADE, strategyTradeDTOList);
        return Result.getSuccessResult(null);
    }

}
